package com.example.juc;

import java.util.concurrent.TimeUnit;

public class SynchorizedTest implements Runnable{
    private Integer ticketNum=100;
    @Override
    public void run() {
        while (true){
            synchronized (this){
                if(ticketNum>0) {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "票" + ticketNum--);
                } else {
                    System.out.println("票已卖完");
                    break;
                }
            }
        }
    }

//    public synchronized void takingTicket(){
//        if(ticketNum>0) {
//            System.out.println(Thread.currentThread().getName() + "票" + ticketNum--);
//        }
//    }
    public static void main(String[] args) {
        SynchorizedTest synchorizedTest = new SynchorizedTest();
        new Thread(synchorizedTest,"A").start();
        new Thread(synchorizedTest,"B").start();
        new Thread(synchorizedTest,"C").start();
        new Thread(synchorizedTest,"D").start();
        new Thread(synchorizedTest,"E").start();
        new Thread(synchorizedTest,"F").start();
    }
}
